package com.example.epic.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.epic.bean.Student;
import com.example.epic.bean.WeChatUser;
import com.example.epic.common.ResponseData;
import com.example.epic.service.StudentService;
import com.example.epic.utils.NetworkUtils;
import com.example.epic.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/wx")
public class WeChatController {

    @Autowired
    StudentService studentService;

    @Autowired
    RedisUtils redisUtils;


    /**
     * 将微信号和学号信息进行绑定。
     * @param weChatUser 携带了学号、密码、姓名、code的信息
     * @param code
     * @return
     */
    @PostMapping("/bind")
    @ResponseBody
    public ResponseData bind(WeChatUser weChatUser, @RequestParam String code){
        String weChatAcessInfo = NetworkUtils.getWeChatAcessInfo(code);
        JSONObject weChatAcessInfoJSON = JSONObject.parseObject(weChatAcessInfo);
        String openid = (String) weChatAcessInfoJSON.get("openid");

        System.out.println("weChatAcessInfo = " + weChatAcessInfo);
        System.out.println("openid = " + openid);
        System.out.println("weChatUser = " + weChatUser);

        // weChatUser包含了学号，密码，code三个信息。
        String userId = weChatUser.getUserId();

        // 可先判断是学生的学号还是教职工的工号
        if(userId.charAt(0) == 't'){
            System.out.println("通过教师的dao查询");

        }
        else{
            Student student = studentService.findStudentByStudentId(userId);

            // 通过学号查找学生，然后判断密码是否正确，最后再绑定openid
            if(student == null)
                return ResponseData.fail("学号不存在");
            if(!weChatUser.getPassword().equals(student.getPassword()))
                return ResponseData.fail("密码错误");

            // 表的openid字段进行更新
            weChatUser.setOpenId(openid);
            // 放进redis
            redisUtils.set(openid, student);
            // 学号和openid进行绑定
            studentService.bindWeChat(weChatUser);
        }

        return ResponseData.success("绑定成功");

    }


    /**
     * 登录，先通过code查询openid，再通过openid查询。
     * @param code
     * @return
     */
    @GetMapping("/login")
    @ResponseBody
    public ResponseData login(@RequestParam String code){
        System.out.println("code = " + code);

        String weChatAcessInfo = NetworkUtils.getWeChatAcessInfo(code);

        JSONObject weChatAcessInfoJSON = JSONObject.parseObject(weChatAcessInfo);

        String openid = (String) weChatAcessInfoJSON.get("openid");
        System.out.println("openid = " + openid);

        // 如果redis里有openid，直接返回
        Student studentRedis = (Student) redisUtils.get(openid);
        if(studentRedis != null)
            return ResponseData.success("登录成功", studentRedis);

        // redis里面没有openid，就去查数据库
        Student student = studentService.findStudentByOpenid(openid);
        // 数据库没有这个openid，没有绑定
        if(student == null)
            return ResponseData.fail("用户未进行绑定");

        // 有这个openid，但没有放进redis。
        System.out.println("student = " + student);
        redisUtils.set(openid, student);
        return ResponseData.success("登录成功", student);
    }


}
